package com.funong.matrix.biz.massage.domain;

import com.funong.matrix.biz.base.dao.SysInmailDao;
import com.funong.matrix.biz.base.dao.SysMsgTemplateDao;
import com.funong.matrix.biz.base.entity.SysInmailEntity;
import com.funong.matrix.biz.base.entity.SysMsgBusinessTemplateEntity;
import com.funong.matrix.biz.base.entity.SysMsgTemplateEntity;
import com.funong.matrix.biz.massage.pojo.BusinessTemplateDTO;
import com.funong.matrix.core.dto.QueryDTO;
import com.funong.matrix.core.dto.Result;
import com.funong.matrix.core.utils.DateTimeUtil;
import com.funong.matrix.core.utils.JwtUtils;
import com.funong.matrix.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : NaNa
 * @since : 2023-05-19 16:56
 **/
@Slf4j
@Service
public class SysMsgTemplateDomainDao {

    @Resource
    private SysMsgTemplateDao templateDao;

    public Result queryMsgTemplate(QueryDTO<SysMsgTemplateEntity> queryDTO) {
        return Result.success(templateDao.queryTemplate(queryDTO));
    }

    public Result updateMsgTemplateStatus(Integer id, Integer status) {
        SysMsgTemplateEntity msgTemplate = templateDao.getById(id);
        String updMsg = "";
        if (msgTemplate == null) {
            updMsg = "记录不存在，更新失败";
        } else {
            msgTemplate.setStatus(status);
            String currentUserName = JwtUtils.getCurrentUserName();
            if(StringUtils.isNotBlank(currentUserName)){
                msgTemplate.setUpdatedBy(currentUserName);
            }
            msgTemplate.setUpdatedAt(DateTimeUtil.now());
            boolean flag = templateDao.updateById(msgTemplate);
            if (flag)
                updMsg = "更新成功";
            else
                updMsg = "更新失败";
        }
        return Result.success(updMsg);
    }

    public Result updateMsgTemplate(SysMsgTemplateEntity msgTemplate) {
        SysMsgTemplateEntity target = templateDao.getById(msgTemplate.getId());
        String updMsg = "";

        if (null != target) {
            target.setTitle(msgTemplate.getTitle());
            target.setContent(msgTemplate.getContent());
            boolean updFlag = templateDao.updateById(msgTemplate);
            if (updFlag)
                updMsg = "更新成功";
            else
                updMsg = "更新失败";
        } else {
            updMsg = "记录不存在，更新失败";
        }
        return Result.success(updMsg);
    }

    List<BusinessTemplateDTO> findTemplateDTO(List<SysMsgBusinessTemplateEntity> businessTemplateList) {
        List<BusinessTemplateDTO> returnInfoDTOS = new ArrayList<>();
        for (SysMsgBusinessTemplateEntity entity : businessTemplateList) {
            BusinessTemplateDTO returnInfoDTO = new BusinessTemplateDTO();

            BeanUtils.copyProperties(entity, returnInfoDTO);
            SysMsgTemplateEntity template = templateDao.getById(entity.getTemplateId());
            returnInfoDTO.setTemplateEntity(template);

            returnInfoDTOS.add(returnInfoDTO);
        }

        return returnInfoDTOS;
    }
}
